﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace Tools
{
   public class 打印手工码单
    {
        public DanjuTable danju { get; set; } = new DanjuTable();
        public List<ManualCode> juanhaolist { get; set; } = new List<ManualCode>();
        public FormInfo formInfo { get; set; } = new FormInfo();
        public List<danjumingxitable> danjumingxitables { get; set; } = new List<danjumingxitable>();
        public FormInfo mingxiinfo { get; set; } = new FormInfo();
		public void Print(string path, PrintModel model)
		{
			DataSet ds = new DataSet();
			ds.Tables.Add(CreateDanjuDatatable.CreateTable(danju, formInfo, "单据信息"));
			ds.Tables.Add(CreateDanjuDatatable.CreateTable(danjumingxitables, mingxiinfo));
			ds.Tables.Add(CreateManualCode());
			var fs = new FastReport.Report();
			fs.RegisterData(ds);
			try
			{
				fs.Load(path);
				switch (model)
				{
					case PrintModel.Design:
						fs.Design();
						var arr = path.Split('\\');
						Connect.dbcontext.Deleteable<ReportTable>().Where(x => x.reportName == arr[arr.Length - 1] && x.reportStyle == ReportService.报表).ExecuteCommand();
						ReportService.LoadReport(path, new ReportTable { reportStyle = ReportService.报表, reportName = arr[arr.Length - 1] });
						break;
					case PrintModel.Privew:
						fs.Show();
						break;
					case PrintModel.Print:
						fs.Print();
						break;
				}
			}
			catch
			{
				fs.Design();
			}
		}
		public DataTable CreateManualCode()
        {
			var dt = new DataTable("细码明细");
			dt.Columns.Add("缸号");
			dt.Columns.Add("颜色");
			dt.Columns.Add("款号");
			dt.Columns.Add("花型");
			dt.Columns.Add("订单号");
			dt.Columns.Add("合计金额");
            for(var col=1;col<11;col++)
            {
				dt.Columns.Add("col"+col.ToString(), typeof(decimal));

            }
            dt.Columns.Add("卷数", typeof(int));
            dt.Columns.Add("合计米数",typeof(decimal));
			foreach(var m in juanhaolist.Where(x=>x.Col1>0).ToList())
            {
				var row = dt.NewRow();
				row["缸号"] = m.Lot;
				row["颜色"] = m.Color;
				row["款号"] = m.ItemNo;
				row["花型"] = danjumingxitables.FirstOrDefault(x => x.ganghao == m.Lot && x.yanse == m.Color).Huahao ;
				row["col1"] = m.Col1;
				row["col2"] = m.Col2;
				row["col3"] = m.Col3;
				row["col4"] = m.Col4;
				row["col5"] = m.Col5;
				row["col6"] = m.Col6;
				row["col7"] = m.Col7;
				row["col8"] = m.Col8;
				row["col9"] = m.Col9;
				row["col10"] = m.Col10;
                row["订单号"] = danjumingxitables.FirstOrDefault(x=>x.ganghao==m.Lot&&x.yanse==m.Color).OrderNum;
                row["卷数"] = CountNonZeroProperties(m);
				row["合计金额"] = danjumingxitables.FirstOrDefault(x => x.ganghao == m.Lot && x.yanse == m.Color).hanshuidanjia * m.SumCol;
                row["合计米数"] = m.SumCol;
				dt.Rows.Add(row);
			}
			return dt;
		}
        private int CountNonZeroProperties(ManualCode obj)
        {
            int count = 0;
            PropertyInfo[] properties = typeof(ManualCode).GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (property.Name.Contains("Col")&&property.Name!="SumCol"&&property.PropertyType==typeof(decimal))
                {
                    decimal value = (decimal)property.GetValue(obj);
                    if (value != 0) count++;
                }
            }
            return count;
        }
    }
}
